<template>
  <div class="container">
    <div class="main">
      <div class="pay-title">
        支付总金额 <span class="pay-price">￥ {{ info.price }}</span>
      </div>
      <div class="pay-main">
        <h4>微信支付</h4>
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          class="pay-qrcode"
        >
          <div class="qrcode" v-if="isshow">
            <!-- 二维码：canvas标签在里面添加默认的内容是无效的，因为这的内容只能被绘制出来 -->
            <canvas id="qrcode-stage"></canvas>
            <p>请使用微信扫一扫</p>
            <p>扫描二维码支付</p>
          </div>
          <div class="qrcode" v-else>
            <span>支付成功，单击跳转到订单详情页</span>
          </div>
          <div class="pay-example">
            <img src="http://157.122.54.189:9093/images/wx-sweep2.jpg" alt="" />
          </div>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import QRCode from "qrcode";
export default {
  data() {
    return {
      info: {},
      timeid: "",
      isshow: true,
    };
  },
  methods: {
    checkPay() {
      // JiPiao - 查询付款状态 http:127.0.0.1:1337/airorders/checkpay
      this.$axios({
        method: "post",
        url: "/airorders/checkpay",
        headers: {
          Authorization: "Bearer " + this.$store.state.user.userinfo.token,
        },
        data: {
          id: this.info.id,
          nonce_str: this.info.payInfo.nonce_str,
          out_trade_no: this.info.payInfo.order_no,
        },
      }).then((res) => {
        console.log(res);
        if (res.data.trade_state == "SUCCESS") {
          this.$message.success("支付成功");
          this.isshow = false;
          clearInterval(this.timeid);
        }
      });
    },
  },
  // 当组件被销毁的时候自动触发
  destroyed() {
    clearInterval(this.timeid);
  },
  mounted() {
    let id = this.$route.query.id;
    // JiPiao - 订单详情 http:127.0.0.1:1337/airorders/:id
    this.$axios({
      url: `/airorders/${id}`,
      headers: {
        Authorization: "Bearer " + this.$store.state.user.userinfo.token,
      },
    }).then((res) => {
      console.log(res);
      this.info = res.data;
      // 根据后台所返回的数据生成二维码
      // 这个容器只能是canvas标签
      // QRCode.toCanvas(dom容器,源数据,[配置],[生成失败的回调]) []代表参数可有可无
      let con = document.getElementById("qrcode-stage");
      // 将指定的内容生成二维码绘制到指定的容器中
      QRCode.toCanvas(con, this.info.payInfo.code_url, {
        width: 200,
      });
      // 轮询用户是否付款
      this.timeid = setInterval(() => {
        this.checkPay();
      }, 4000);
    });
  },
};
</script>

<style scoped lang="less">
.container {
  background: #f5f5f5;
  padding: 30px 0;

  .main {
    width: 1000px;
    margin: 0 auto;

    .pay-title {
      text-align: right;
      span {
        font-size: 28px;
        color: orangered;
      }
    }

    .pay-main {
      background: #fff;
      margin-top: 10px;
      border-top: 5px orange solid;
      padding: 30px;

      h4 {
        font-size: 28px;
        font-weight: normal;
        margin-bottom: 10px;
      }

      .pay-qrcode {
        padding: 0 80px;
      }

      .qrcode {
        border: 1px #ddd solid;
        padding: 15px;
        height: fit-content;

        #qrcode-stage {
          width: 200px;
          height: 200px;
          margin-bottom: 10px;
        }

        p {
          line-height: 2;
          text-align: center;
        }
      }
    }
  }
}
</style>